package flexcommon.tests.utils
{
	import flexunit.framework.Assert;
	import flexunit.framework.TestCase;
	
	import flexcommon.utils.StringUtils;

	/**
	 * @private
	 */
	public class TestStringUtils extends TestCase
	{
		public function TestStringUtils(methodName:String=null)
		{
			super(methodName);
		}
		
		public override function setUp():void {
	    	
	  }
		
		public function testAbbreviate():void 
		{
      try {
      	Assert.assertEquals( "Hello World", StringUtils.abbreviate( "Hello World", 98 ) );
      	Assert.assertEquals( "Hello...", StringUtils.abbreviate( "Hello World", 8 ) );
      }
      catch( e:Error ) {
      	trace( "testAbbreviate >> " + e.getStackTrace() );
      	fail( "testAbbreviate failed: " + e.message );
      }
    }
    
    public function testCapitalize():void 
    {
      try {
        Assert.assertEquals( null, StringUtils.capitalize( null ) );
        Assert.assertEquals( "", StringUtils.capitalize( "" ) );
        Assert.assertEquals( "Hello", StringUtils.capitalize( "hello" ) );
      }
      catch( e:Error ) {
        trace( "testCapitalize >> " + e.getStackTrace() );
        fail( "testCapitalize failed: " + e.message );
      }
    }
	    
	  public function testCompare():void 
		{
      try {
      	Assert.assertEquals( 0, StringUtils.compare( "Hello World", "Hello World" ) );
      	Assert.assertEquals( 1, StringUtils.compare( "Hello World", "A new world" ) );
      	Assert.assertEquals( 1, StringUtils.compare( "Hello World", "90210" ) );
      	Assert.assertEquals( -1, StringUtils.compare( "Hello World", "Zulu time" ) );
      }
      catch( e:Error ) {
      	trace( "testCompare >> " + e.getStackTrace() );
      	fail( "testCompare failed: " + e.message );
      }
    }
	    
	  public function testContains():void 
		{
      try {
      	Assert.assertEquals( true, StringUtils.contains( "Pink Floyd", "Pink" ) );
      	Assert.assertEquals( false, StringUtils.contains( "Pink Floyd", "Zeppelin" ) );
      }
      catch( e:Error ) {
      	trace( "testContains >> " + e.getStackTrace() );
      	fail( "testContains failed: " + e.message );
      }
    }
	    
	  public function testCount():void 
		{
      try {
      	Assert.assertEquals( 1, StringUtils.count( "Pink Floyd", "Pink" ) );
      	Assert.assertEquals( 0, StringUtils.contains( "Pink Floyd", "Zeppelin" ) );
      	Assert.assertEquals( 0, StringUtils.contains( null, null ) );
      }
      catch( e:Error ) {
      	trace( "testCount >> " + e.getStackTrace() );
      	fail( "testCount failed: " + e.message );
      }
    }
	    
	  public function testDefaultString():void 
		{
      try {
      	Assert.assertEquals( "Pink Floyd", StringUtils.defaultString( "Pink Floyd" ) );
      	Assert.assertEquals( "", StringUtils.defaultString( "" ) );
      	Assert.assertEquals( "", StringUtils.defaultString( null ) );
      }
      catch( e:Error ) {
      	trace( "testDefaultString >> " + e.getStackTrace() );
      	fail( "testDefaultString failed: " + e.message );
      }
    }
    
    public function testIsAlpha():void 
    {
      try {
        Assert.assertEquals( false, StringUtils.isAlpha( "Pink Floyd" ) );
        Assert.assertEquals( false, StringUtils.isAlpha( "123" ) );
        Assert.assertEquals( false, StringUtils.isAlpha( "ABC123" ) );
        Assert.assertEquals( true, StringUtils.isAlpha( "" ) );
        Assert.assertEquals( false, StringUtils.isAlpha( null ) );
      }
      catch( e:Error ) {
        trace( "testIsAlpha >> " + e.getStackTrace() );
        fail( "testIsAlpha failed: " + e.message );
      }
    }
    
    public function testIsAlphaNumeric():void 
    {
      try {
        Assert.assertEquals( false, StringUtils.isAlphaNumeric( "Pink Floyd" ) );
        Assert.assertEquals( true, StringUtils.isAlphaNumeric( "123" ) );
        Assert.assertEquals( true, StringUtils.isAlphaNumeric( "ABC" ) );
        Assert.assertEquals( true, StringUtils.isAlphaNumeric( "ABC123" ) );
        Assert.assertEquals( true, StringUtils.isAlphaNumeric( "" ) );
        Assert.assertEquals( false, StringUtils.isAlphaNumeric( null ) );
      }
      catch( e:Error ) {
        trace( "testIsAlphaNumeric >> " + e.getStackTrace() );
        fail( "testIsAlphaNumeric failed: " + e.message );
      }
    }
	    
	  public function testIsEmpty():void 
		{
      try {
      	Assert.assertEquals( false, StringUtils.isEmpty( "Pink Floyd" ) );
      	Assert.assertEquals( true, StringUtils.isEmpty( "" ) );
      	Assert.assertEquals( true, StringUtils.isEmpty( null ) );
      }
      catch( e:Error ) {
      	trace( "testIsEmpty >> " + e.getStackTrace() );
      	fail( "testIsEmpty failed: " + e.message );
      }
    }
	    
	  public function testIsNotEmpty():void 
		{
      try {
      	Assert.assertEquals( true, StringUtils.isNotEmpty( "Pink Floyd" ) );
      	Assert.assertEquals( false, StringUtils.isNotEmpty( "" ) );
      	Assert.assertEquals( false, StringUtils.isNotEmpty( null ) );
      }
      catch( e:Error ) {
      	trace( "testIsNotEmpty >> " + e.getStackTrace() );
      	fail( "testIsNotEmpty failed: " + e.message );
      }
    }
      
    public function testIsNumeric():void
    {
      try {
        Assert.assertEquals( false, StringUtils.isNumeric( "Pink Floyd" ) );
        Assert.assertEquals( false, StringUtils.isNumeric( "" ) );
        Assert.assertEquals( true, StringUtils.isNumeric( "123456" ) );
      }
      catch( e:Error ) {
        trace( "testIsNumeric >> " + e.getStackTrace() );
        fail( "testIsNumeric failed: " + e.message );
      }
    }
    
    public function testLeft():void
    {
      try {
        Assert.assertEquals( null, StringUtils.left( null, 5 ) );
        Assert.assertEquals( "", StringUtils.left( "Hello World", -5 ) );
        Assert.assertEquals( "Hello", StringUtils.left( "Hello World", 5 ) );
        Assert.assertEquals( "Hello World", StringUtils.left( "Hello World", 99 ) );
      }
      catch( e:Error ) {
        trace( "testLeft >> " + e.getStackTrace() );
        fail( "testLeft failed: " + e.message );
      }
    }
	    
    public function testLeftPad():void 
		{
      try {
      	Assert.assertEquals( "Hello World", StringUtils.leftPad( "Hello World", -1 ) );
      	Assert.assertEquals( "00000Hello World", StringUtils.leftPad( "Hello World", 5 ) );
      	Assert.assertEquals( "zzzzzHello World", StringUtils.leftPad( "Hello World", 5, "z" ) );
      }
      catch( e:Error ) {
      	trace( "testLeftPad >> " + e.getStackTrace() );
      	fail( "testLeftPad failed: " + e.message );
      }
    }
    
    public function testLowercase():void 
    {
      try {
        Assert.assertEquals( null, StringUtils.lowercase( null ) );
        Assert.assertEquals( "", StringUtils.lowercase( "" ) );
        Assert.assertEquals( "hello world", StringUtils.lowercase( "Hello World" ) );
      }
      catch( e:Error ) {
        trace( "testLowercase >> " + e.getStackTrace() );
        fail( "testLowercase failed: " + e.message );
      }
    }
    
    public function testReplaceAll():void 
		{
      try {
      	Assert.assertEquals( "Hello Ladies", StringUtils.replaceAll( "Hello World", "World", "Ladies" ) );
      	Assert.assertEquals( "Hello World", StringUtils.replaceAll( "Hello World", "", "" ) );
      	Assert.assertEquals( "Hello World", StringUtils.replaceAll( "Hello World", "Dude", "Ladies" ) );
      }
      catch( e:Error ) {
      	trace( "testReplaceAll >> " + e.getStackTrace() );
      	fail( "testReplaceAll failed: " + e.message );
      }
    }
	    
	  public function testReplaceFirst():void 
		{
      try {
      	Assert.assertEquals( "Hello Ladies World", StringUtils.replaceFirst( "Hello World World", "World", "Ladies" ) );
      	Assert.assertEquals( "Hello World", StringUtils.replaceFirst( "Hello World", "", "" ) );
      	Assert.assertEquals( "Hello World", StringUtils.replaceFirst( "Hello World", "Dude", "Ladies" ) );
      }
      catch( e:Error ) {
      	trace( "testReplaceFirst >> " + e.getStackTrace() );
      	fail( "testReplaceFirst failed: " + e.message );
      }
    }
      
    public function testRight():void
    {
      try {
        Assert.assertEquals( null, StringUtils.right( null, 5 ) );
        Assert.assertEquals( "", StringUtils.right( "Hello World", -5 ) );
        Assert.assertEquals( "World", StringUtils.right( "Hello World", 5 ) );
        Assert.assertEquals( "Hello World", StringUtils.right( "Hello World", 99 ) );
      }
      catch( e:Error ) {
        trace( "testRight >> " + e.getStackTrace() );
        fail( "testRight failed: " + e.message );
      }
    }
	    
	  public function testStartsWith():void 
		{
      try {
      	Assert.assertEquals( true, StringUtils.startsWith( "Hello World World", "Hello" ) );
      	Assert.assertEquals( false, StringUtils.startsWith( "Hello World", "" ) );
      	Assert.assertEquals( false, StringUtils.startsWith( "Hello World", "Dude" ) );
      }
      catch( e:Error ) {
      	trace( "testStartsWith >> " + e.getStackTrace() );
      	fail( "testStartsWith failed: " + e.message );
      }
    }
	    
	  public function testSubstringAfter():void 
		{
      try {
      	Assert.assertEquals( " young Skywalker", 
      		StringUtils.substringAfter( "The Force is with you young Skywalker", "you" ) );
      	Assert.assertEquals( "The Force is with you young Skywalker", 
      		StringUtils.substringAfter( "The Force is with you young Skywalker", "blue" ) );
      	Assert.assertEquals( "The Force is with you young Skywalker", 
      		StringUtils.substringAfter( "The Force is with you young Skywalker", "" ) );
      }
      catch( e:Error ) {
      	trace( "testSubstringAfter >> " + e.getStackTrace() );
      	fail( "testSubstringAfter failed: " + e.message );
      }
    }
	    
	  public function testSubstringAfterLast():void 
		{
      try {
      	Assert.assertEquals( " rebell alliance", 
      		StringUtils.substringAfterLast( "the Force is the rebell alliance", "the" ) );
      	Assert.assertEquals( "the Force is the rebell alliance", 
      		StringUtils.substringAfterLast( "the Force is the rebell alliance", "blue" ) );
      	Assert.assertEquals( "the Force is the rebell alliance", 
      		StringUtils.substringAfterLast( "the Force is the rebell alliance", "" ) );
      }
      catch( e:Error ) {
      	trace( "testSubstringAfterLast >> " + e.getStackTrace() );
      	fail( "testSubstringAfterLast failed: " + e.message );
      }
    }
	    
	  public function testSubstringBefore():void 
		{
      try {
      	Assert.assertEquals( "the Force ", 
      		StringUtils.substringBefore( "the Force is the rebell alliance", "is" ) );
      	Assert.assertEquals( "the Force is the rebell alliance", 
      		StringUtils.substringBefore( "the Force is the rebell alliance", "blue" ) );
      	Assert.assertEquals( "the Force is the rebell alliance", 
      		StringUtils.substringBefore( "the Force is the rebell alliance", "" ) );
      }
      catch( e:Error ) {
      	trace( "testSubstringBefore >> " + e.getStackTrace() );
      	fail( "testSubstringBefore failed: " + e.message );
      }
    }
	    
	  public function testTokenize():void 
		{
      try {
      	var array:Array = StringUtils.tokenize( "the Force is the rebell alliance", " " );
      	
      	Assert.assertEquals( 6, array.length );
      	Assert.assertEquals( "the", array[0] );
      	Assert.assertEquals( "Force", array[1] );
      	Assert.assertEquals( "is", array[2] );
      	Assert.assertEquals( "the", array[3] );
      	Assert.assertEquals( "rebell", array[4] );
      	Assert.assertEquals( "alliance", array[5] );
      	
      	array = StringUtils.tokenize( "the Force is the rebell alliance", "blue" );
      	
      	Assert.assertEquals( 0, array.length );
      	
      	array = StringUtils.tokenize( "the Force is the rebell alliance", "" );
      	
      	Assert.assertEquals( 0, array.length );
      }
      catch( e:Error ) {
      	trace( "testTokenize >> " + e.getStackTrace() );
      	fail( "testTokenize failed: " + e.message );
      }
    }
	    
	  public function testTrim():void 
		{
      try {
      	Assert.assertEquals( "the Force is the rebell alliance", 
      		StringUtils.trim( "the Force is the rebell alliance" ) );
      	Assert.assertEquals( "the Force is the rebell alliance", 
      		StringUtils.trim( "   the Force is the rebell alliance   " ) );
      }
      catch( e:Error ) {
      	trace( "testTrim >> " + e.getStackTrace() );
      	fail( "testTrim failed: " + e.message );
      }
    }
    
    public function testUppercase():void 
    {
      try {
        Assert.assertEquals( null, StringUtils.uppercase( null ) );
        Assert.assertEquals( "", StringUtils.uppercase( "" ) );
        Assert.assertEquals( "HELLO WORLD", StringUtils.uppercase( "Hello World" ) );
      }
      catch( e:Error ) {
        trace( "testUppercase >> " + e.getStackTrace() );
        fail( "testUppercase failed: " + e.message );
      }
    }
	}
}